package cloud.dandanit.itservice.controller.hyperlink;


import cloud.dandanit.itservice.entity.Article;
import cloud.dandanit.itservice.entity.Hyperlink;
import cloud.dandanit.itservice.service.HyperlinkService;
import cloud.dandanit.itservice.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程简介 前端控制器
 * </p>
 *
 * @author luokai
 * @since 2022-07-05
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/itservice/hyperlinkadmin")
public class HyperlinkAdminController {

    @Autowired
    private HyperlinkService hyperlinkService;

    @ApiOperation(value = "获取文章分页列表")
    @GetMapping("pageHyperlink/{page}/{limit}")
    public R getPageHyperlink(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {

        Page<Hyperlink> pageParam = new Page<>(page, limit);
        LambdaQueryWrapper<Hyperlink> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Hyperlink::getSort);
        hyperlinkService.page(pageParam,queryWrapper);
        return R.ok().data("items", pageParam.getRecords()).data("total", pageParam.getTotal());
    }

    @ApiOperation(value = "新增超链接")
    @PostMapping("addHyperlink")
    public R save(
            @ApiParam(name = "hyperlink", value = "超链接实体", required = true)
            @RequestBody Hyperlink hyperlink) {

        hyperlinkService.save(hyperlink);
        return R.ok();
    }

    @ApiOperation(value = "根据id获取超链接")
    @GetMapping("getHyperlinkById/{id}")
    public R getHyperlinkById(
            @ApiParam(name = "id", value = "超链接ID", required = true)
            @PathVariable String id) {

        Hyperlink hyperlink = hyperlinkService.getById(id);
        return R.ok().data("hyperlink", hyperlink);
    }

    @ApiOperation(value = "修改超链接")
    @PostMapping("update")
    public R update(
            @ApiParam(name = "hyperlink", value = "超链接实体", required = true)
            @RequestBody Hyperlink hyperlink) {

        hyperlinkService.updateById(hyperlink);
        return R.ok();
    }

    @ApiOperation(value = "删除超链接")
    @DeleteMapping("removeHyperlinkById/{id}")
    public R removeHyperlinkById(
            @ApiParam(name = "id", value = "超链接ID", required = true)
            @PathVariable String id) {

        hyperlinkService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "搜索超链接")
    @GetMapping("searchHyperlink/{keyword}")
    public R searchHyperlink(
            @ApiParam(name = "keyword", value = "关键字", required = true)
            @PathVariable String keyword) {

        //空搜索框搜索全部
        if ("null".equals(keyword)) {
            return R.ok().data("searchResult",hyperlinkService.list(null));
        }

        //查询title字段
        QueryWrapper<Hyperlink> queryWrapperTitle = new QueryWrapper<>();
        queryWrapperTitle.like("title", keyword);
        List<Hyperlink> titleList = hyperlinkService.list(queryWrapperTitle);

        //查询linkUrl字段
        QueryWrapper<Hyperlink> queryWrapperLinkUrl = new QueryWrapper<>();
        queryWrapperLinkUrl.like("link_url", keyword);
        List<Hyperlink> linkUrlList = hyperlinkService.list(queryWrapperLinkUrl);
        //求并集
        titleList.addAll(linkUrlList);
        //集合去重
        List<Hyperlink> resultList = titleList.stream().distinct().collect(Collectors.toList());

        return R.ok().data("searchResult",resultList);
    }


    @ApiOperation(value = "批量删除超链接")
    @DeleteMapping("batchDelete")
    public R batchDelete(
            @ApiParam(name = "ids", value = "超链接ID集合", required = true)
            @RequestBody List<String> ids) {

        hyperlinkService.removeByIds(ids);
        return R.ok();
    }

    @ApiOperation(value = "更改超链接状态")
    @GetMapping("changeState/{hyperlinkId}")
    public R changeState(
            @ApiParam(name = "hyperlinkId", value = "轮播图ID", required = true)
            @PathVariable String hyperlinkId) {

        Hyperlink hyperlink = hyperlinkService.getById(hyperlinkId);
        if (hyperlink.getState()==0) {
            hyperlink.setState(1);
        } else {
            hyperlink.setState(0);
        }
        hyperlinkService.updateById(hyperlink);

        return R.ok();
    }

    @ApiOperation(value = "一键修改超链接状态")
    @GetMapping("batchChangeHyperlinkState/{state}")
    public R batchChangeHyperlinkState(
            @ApiParam(name = "state", value = "更改的状态", required = true)
            @PathVariable Integer state) {

        List<Hyperlink> hyperlinkList = hyperlinkService.list(null);
        for (Hyperlink hyperlink : hyperlinkList) {
            hyperlink.setState(state);
        }
        hyperlinkService.updateBatchById(hyperlinkList);
        return R.ok();
    }

}

